import {ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, OnInit} from '@angular/core';
import {FormArray, FormBuilder, FormGroup, Validators} from "@angular/forms";
import {NzMessageService} from "ng-zorro-antd/message";
import {ValidatorsService} from "../../../../core/services/validators/validators.service";
import {fnCheckForm} from "../../../../utils/tools";
import {ElementsDto, MsdsElementHttpService} from "../../../../core/services/http/msds/msds-element.service";
import {MsdsIdService} from "../../../../core/services/store/biz-store-service/msds-id/msds-id.service";
import {AdminService} from "../../../../core/services/store/admin.service";


@Component({
  selector: 'app-msds-element',
  templateUrl: './msds-element.component.html',
  styleUrls: ['./msds-element.component.less'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class MsdsElementComponent implements OnInit {
  validateForm!: FormGroup;
  formOrder = 1;
  msdsEntprId!: number;
  isAdd = false;
  id!: number;

  constructor(private fb: FormBuilder, private msg: NzMessageService,
              private validatorsService: ValidatorsService,
              private cdr: ChangeDetectorRef,
              private msdsIdService: MsdsIdService, public AdminService: AdminService,
              private dataService: MsdsElementHttpService) {
  }

  buttonChange(e: boolean): void {
    this.items.clear();
    const len = e ? 1 : 4;
    for (let i = 0; i < len; i++) {
      this.items.push(this.creatForm());
    }
  }

  creatForm(): FormGroup {
    return this.fb.group(({
      cas: [null],
      harmfulElement: [null],
      harmfulMmol: [null],
      dangerNo: [null],
    }))
  }

  initForm(): void {
    this.validateForm = this.fb.group({
      pureFlag: [null, [Validators.required]],
      elementDetailDtos: this.fb.array([]),
    });
  }

  get items() {
    return this.validateForm.controls['elementDetailDtos'] as FormArray;
  }

  submitForm(): void {
    if (!fnCheckForm(this.validateForm)) {
      return;
    }
    /*过滤混合物空的对象数组*/
    const params = this.validateForm.value;
    params.elementDetailDtos = params.elementDetailDtos.filter((item: ElementsDto, itemIndex: any, itemArr: any) => {
      return itemArr[itemIndex].dangerNo != null || itemArr[itemIndex].cas != null || itemArr[itemIndex].harmfulElement != null || itemArr[itemIndex].harmfulMmol != null
    });
    if (this.isAdd) {
      params.msdsEntprId = this.msdsEntprId
      this.dataService.addMsdsElement(params).subscribe(res => {
        this.id = res;
        this.isAdd = false;
      });
    } else {
      params.id = this.id;
      this.dataService.editMsdsElement(params).subscribe()
    }
  }


  getDetail(msdsEntprId: number): void {
    this.dataService.getMsdsElementDetail(msdsEntprId).subscribe(res => {
      this.isAdd = !res;
      if (!!res) {
        this.id = res.id as number;
        this.validateForm.get('pureFlag')?.setValue(res.pureFlag);
        this.buttonChange(res.pureFlag);/*触发初始化formarray 每一项*/
        this.items.patchValue(res.elementDetails);
      }
    })
  }

  ngOnInit(): void {
    this.initForm();
    this.msdsIdService.getMsdsIdStoreStore().subscribe(res => this.msdsEntprId = res);
    this.getDetail(this.msdsEntprId);
  }

}
